package smartbooks.ui.helper;

import smartbooks.exceptions.MyException;
import smartbooks.ui.util.CommonConstants;
import smartbooks.ui.util.CommonFunctions;
import smartbooksdb.hibernatebean.Customer;

/**
 *
 * @author Ankit
 */
public class CustomerHelper implements CommonConstants {
    /**
     * This method is used to validate the customer panel
     * @param customer
     * @return 'true' if customer panel is validated or else 'false'
     * @throws MyException
     */
    public static boolean validateCustomer(Customer customer) throws MyException {
        boolean customerPanelValidated = true;
        
        if(null==customer.getCustomerName() || customer.getCustomerName().length()==0) {
            throw new MyException(CUSTOMER_NAME, "Customer name cannot be blank");
        }
        
        if(null!=customer.getBillToState() && customer.getBillToState().length()>32) {
            throw new MyException(BILL_TO_STATE, "Bill To State should have maximum 32 characters");
        }
        
        if(null!=customer.getBillToCity() && customer.getBillToCity().length()>32) {
            throw new MyException(BILL_TO_CITY, "Bill To City should have maximum 32 characters");
        }
        
        if(null!=customer.getShipToState() && customer.getShipToState().length()>32) {
            throw new MyException(SHIP_TO_STATE, "Ship To State should have maximum 32 characters");
        }
        
        if(null!=customer.getShipToCity() && customer.getShipToCity().length()>32) {
            throw new MyException(SHIP_TO_CITY, "Ship To City should have maximum 32 characters");
        }
        
        if(!customer.getGender().equals(GENDER_MALE) && !customer.getGender().equals(GENDER_FEMALE)) {
            throw new MyException(GENDER, "Please select a Gender");
        }
        
        if(null!=customer.getEmail() && !CommonFunctions.validateEmail(customer.getEmail())) {
            throw new MyException(EMAIL, "Invalid Email");
        }
        
        return customerPanelValidated;
    }
}